﻿@model IEnumerable<LotteryVoteMVC.Models.BetLimit>
@using LotteryVoteMVC.Core
@using LotteryVoteMVC.Resources
@using LotteryVoteMVC.Models
@using LotteryVoteMVC.Utility
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Edit Limit</title>
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Customer/comm.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Customer/ajaxHelper.js")" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.theme.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/comm.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/TableBase.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/inputs.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/commission.css")" rel="stylesheet" type="text/css" />
</head>
<body>
    <h3>@Resource.MemberBetLimit</h3>
    @using (Html.BeginForm("BetLimit", "Limit", new { Id = Request.RequestContext.RouteData.Values["Id"] }))
    {
        <table>
            <thead>
                <tr>
                    @foreach (var gameType in ViewBag.GameTypes)
                    { 
                        <td colspan="2">@gameType.Description
                        </td>
                    }
                    <td rowspan="2" class="rows">
                        @Resource.Function
                    </td>
                </tr>
                <tr>
                    @for (int i = 0; i < ViewBag.GameTypes.Count; i++)
                    { 
                        <td>@Resource.Min
                        </td>
                        <td>@Resource.Max
                        </td>
                    }
                </tr>
            </thead>
            <tbody>
                <tr>
                    @foreach (var gameType in ViewBag.GameTypes)
                    {
                        var limit = Model.Find(it => it.GameId == gameType.Value);
                        if (limit == null)
                        {
                            limit = new LotteryVoteMVC.Models.BetLimit
                            {
                                GameId = gameType.Value,
                                LeastLimit = 0,
                                LargestLimit = 0
                            };
                        }
                        using (Html.BeginCollectionItem("model"))
                        {
                        <td>
                            @Html.TextBox("LeastLimit", limit.LeastLimit, new { @class = "auto" })
                            @Html.ValidationMessage("LeastLimit")
                        </td>
                        <td>
                            @Html.TextBox("LargestLimit", limit.LargestLimit, new { @class = "auto" })
                            @Html.ValidationMessage("LargestLimit")
                            @Html.Hidden("GameId", limit.GameId)
                        </td>
                        }
                    }
                    <td>
                        @if (this.HasRight((Role)ViewBag.Role - 1, "LimitController", "BetLimit") || (LoginCenter.CurrentUser.Role == Role.Company && ViewBag.Role == Role.Company))
                        {
                            <input type="submit" name="sub_limit" name="sub_limit" value="@Resource.Save" />
                        }
                    </td>
                </tr>
            </tbody>
        </table>
    }
    <h3>@Resource.MemberGameLimit</h3>
    <table>
        <thead>
            <tr>
                <td rowspan="2" class="rows">@Resource.CompanyType
                </td>
                <td colspan="4">
                    2D
                </td>
                <td colspan="4">
                    3D
                </td>
                <td colspan="2">
                    4D
                </td>
                <td>
                    5D
                </td>
                <td rowspan="2" class="rows">
                    PL2
                </td>
                <td rowspan="2" class="rows">
                    PL3
                </td>
                <td rowspan="2" class="rows">
                    A&B PL2
                </td>
                <td rowspan="2" class="rows">
                    B&C PL2
                </td>
                <td rowspan="2" class="rows">
                    C&A PL2
                </td>
                <td rowspan="2" class="rows">@Resource.Function
                </td>
            </tr>
            <tr>
                <td>@Resource.Head
                </td>
                <td>@Resource.Last
                </td>
                <td>@Resource.Roll
                </td>
                <td>@Resource.Roll7
                </td>
                <td>@Resource.Head
                </td>
                <td>@Resource.Last
                </td>
                <td>@Resource.Roll
                </td>
                <td>@Resource.Roll7
                </td>
                <td>@Resource.Last
                </td>
                <td>@Resource.Roll
                </td>
                <td>@Resource.Roll
                </td>
            </tr>
        </thead>
        <tbody>
            @{var gamelimits = ViewBag.GameLimits as IEnumerable<GameBetLimit>;
              var gpws = ViewBag.GamePlayWays as IEnumerable<GamePlayWay>;
              GamePlayWay gpw;
              GameBetLimit glimit;
              bool isFirst = true;
              bool isSecond = false;
              bool isThird = false;
              bool isFour = false;
            }
            @foreach (var comType in ViewBag.CompanyTypes)
            { 
                <tr>
                    @using (Html.BeginForm("GameLimit", "Limit", new { Id = Request.RequestContext.RouteData.Values["Id"], companytype = comType.Value }))
                    {
                        <td>@comType.Description
                        </td>
                        foreach (var gt in new[] { GameType.TwoDigital, GameType.ThreeDigital })
                        {
                            foreach (var pw in new[] { PlayWay.Head, PlayWay.Last, PlayWay.Roll, PlayWay.Roll7 })
                            {
                        <td>
                            @{ gpw = gpws.Find(it => it.GameType == gt && it.PlayWay == pw);
                               glimit = gamelimits.Find(it => it.GamePlayWayId == gpw.Id && (int)it.CompanyType == comType.Value);
                               if (glimit != null)
                               {
                                @Html.Partial("LimitItem", glimit)
                               }}
                        </td>
                            }
                        }
                        <td>
                            @{
                        gpw = gpws.Find(it => it.GameType == GameType.FourDigital && it.PlayWay == PlayWay.Last);
                        glimit = gamelimits.Find(it => it.GamePlayWayId == gpw.Id && (int)it.CompanyType == comType.Value);
                            }
                            @Html.Partial("LimitItem", glimit)
                        </td>
                        <td>
                            @{
                        gpw = gpws.Find(it => it.GameType == GameType.FourDigital && it.PlayWay == PlayWay.Roll);
                        glimit = gamelimits.Find(it => it.GamePlayWayId == gpw.Id && (int)it.CompanyType == comType.Value);
                            }
                            @Html.Partial("LimitItem", glimit)
                        </td>
                        <td>
                            @{
                        gpw = gpws.Find(it => it.GameType == GameType.FiveDigital && it.PlayWay == PlayWay.Roll);
                        glimit = gamelimits.Find(it => it.GamePlayWayId == gpw.Id && (int)it.CompanyType == comType.Value);
                            }
                            @Html.Partial("LimitItem", glimit)
                        </td>
                        <td>
                            @{
                        gpw = gpws.Find(it => it.GameType == GameType.PL2);
                        glimit = gamelimits.Find(it => it.GamePlayWayId == gpw.Id && (int)it.CompanyType == comType.Value);
                            }
                            @Html.Partial("LimitItem", glimit)
                        </td>
                        <td>
                            @{
                        gpw = gpws.Find(it => it.GameType == GameType.PL3);
                        glimit = gamelimits.Find(it => it.GamePlayWayId == gpw.Id && (int)it.CompanyType == comType.Value);
                            }
                            @Html.Partial("LimitItem", glimit)
                        </td>
                        if (isSecond)
                        {
                            gpw = gpws.Find(it => it.GameType == GameType.A_B_PL2);
                            glimit = gamelimits.Find(it => it.GamePlayWayId == gpw.Id);
                            if (glimit == null)
                            {
                                glimit = new GameBetLimit { GamePlayWayId = gpw.Id, CompanyType = CompanyType.EighteenA, LimitValue = 0 };
                            }
                        <td>
                            @Html.Partial("LimitItem", glimit)
                        </td>
                        
                            
                        }
                        else
                        { 
                        <td>
                        </td>
                        }

                        if (isThird)
                        {
                            gpw = gpws.Find(it => it.GameType == GameType.B_C_PL2);
                            glimit = gamelimits.Find(it => it.GamePlayWayId == gpw.Id);
                            if (glimit == null)
                            {
                                glimit = new GameBetLimit { GamePlayWayId = gpw.Id, CompanyType = CompanyType.EighteenB, LimitValue = 0 };
                            }
                        <td>
                            @Html.Partial("LimitItem", glimit)
                        </td>
                        }
                        else
                        { 
                        <td>
                        </td>
                        }

                        if (isFour)
                        {
                            isFour = false;
                            gpw = gpws.Find(it => it.GameType == GameType.C_A_PL2);
                            glimit = gamelimits.Find(it => it.GamePlayWayId == gpw.Id);
                            if (glimit == null)
                            {
                                glimit = new GameBetLimit { GamePlayWayId = gpw.Id, CompanyType = CompanyType.EighteenC, LimitValue = 0 };
                            }
                        <td>
                            @Html.Partial("LimitItem", glimit)
                        </td>
                        }
                        else
                        { 
                        <td>
                        </td>
                        }

                        if (isFirst)
                        {
                            isFirst = false;
                            isSecond = true;
                        }
                        else if (isSecond)
                        {
                            isSecond = false;
                            isThird = true;
                        }
                        else if (isThird)
                        {
                            isThird = false;
                            isFour = true;
                        }
                        
                        <td>
                            @if (this.HasRight((Role)ViewBag.Role - 1, "LimitController", "BetLimit") || (LoginCenter.CurrentUser.Role == Role.Company && ViewBag.Role == Role.Company))
                            {
                                <input type="submit" name="gameSub" id="gameSub" value="@Resource.Save" />
                            }
                        </td>
                        
                    }
                </tr>
            }
        </tbody>
    </table>
</body>
</html>
